﻿using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using Wpf2.Models;
using Wpf2.ViewModels;
using WPFServices.Model;

namespace Wpf2.Views
{
    /// <summary>
    /// Interaction logic for ucOrderManagement.xaml
    /// </summary>
    public partial class UcOrderManagement 
    {
        bool _isExPortOrder; // true : Export, false: Import
        NotifiableCollection<Catalogs> _catalogs;
        ObservableCollection<ModelOrderManagement> _listOfBills = new ObservableCollection<ModelOrderManagement>();

        public UcOrderManagement()
        {
            try
            {
                InitializeComponent();
                dtgOrderManagement.LoadingRow += DtgOrderManagementLoadingRow;
                LoadListCatalogIntoListView();
                LoadListOfOrderManagement();
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }

       
        private void LoadListOfOrderManagement()
        {
            try
            {
                //load với idCatalog = 0; ==> khong so sanh => load het
                _listOfBills = WPFServices.Services.OrderManagementService.GetAllListBillsWithoutDate(0, _isExPortOrder);
                dtgOrderManagement.ItemsSource = _listOfBills;
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }

        private static void DtgOrderManagementLoadingRow(object sender, DataGridRowEventArgs e)
        {
            e.Row.Header = e.Row.GetIndex() + 1;
        }

        private void LoadListCatalogIntoListView()
        {
            try
            {
                _catalogs = new NotifiableCollection<Catalogs>();
                // tạo catalog nay để hiển thị trên giao diện. đại diện cho việc tìm tất cả hóa đơn không xác định catalog nào
                var catalogToFindAll = new Catalogs { CatalogName = "Tất cả", CatalogNotes = "Tất cả", IDCatalog = 0 };
                _catalogs.Add(catalogToFindAll);

                var listCa = WPFServices.Services.Catalogs.GetListCatalogs().Select(x => new Catalogs
                {
                    IDCatalog = x.ID_CATALOG,
                    CatalogName = x.NAME_CATALOG,
                    CatalogNotes = x.NOTES_CATALOG
                });

                foreach (var c in listCa)
                {
                    _catalogs.Add(c);
                }
                lvCatalog.DataContext = _catalogs;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void DpickerSelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            GetListOfOrderWithConditions();
        }

        private void GetListOfOrderWithConditions()
        {
            try
            {
                var catalogID = 0;
                if (lvCatalog.SelectedIndex > 0) // đã lựa chọn 1 catalog
                {
                    var catalogs = lvCatalog.SelectedItem as Catalogs;
                    if (catalogs != null)
                        catalogID = catalogs.IDCatalog;
                }

                if (dpickerFrom.SelectedDate.HasValue && dpickerTo.SelectedDate.HasValue)
                {
                    _listOfBills = WPFServices.Services.OrderManagementService.GetAllListBillsByDate(dpickerFrom.SelectedDate.Value, dpickerTo.SelectedDate.Value, catalogID, _isExPortOrder);
                    dtgOrderManagement.ItemsSource = _listOfBills;
                }
                else
                {
                    _listOfBills = WPFServices.Services.OrderManagementService.GetAllListBillsWithoutDate(catalogID, _isExPortOrder);
                    dtgOrderManagement.ItemsSource = _listOfBills;
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }

        }

        private void LvCatalogSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                GetListOfOrderWithConditions();
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }

        private void BtnSearchByNameClick(object sender, RoutedEventArgs e)
        {
            try
            {
                var catalogID = 0;
                if (lvCatalog.SelectedIndex > 0) // đã lựa chọn 1 catalog
                {
                    var catalogs = lvCatalog.SelectedItem as Catalogs;
                    if (catalogs != null)
                        catalogID = catalogs.IDCatalog;
                }
                _listOfBills = WPFServices.Services.OrderManagementService.GetAllListBillsByCustomerName(txtCustomerName.Text, dpickerFrom.SelectedDate, dpickerTo.SelectedDate, catalogID, _isExPortOrder);
                dtgOrderManagement.ItemsSource = _listOfBills;
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }

        private void RabtnInOrderChecked(object sender, RoutedEventArgs e)
        {
            try
            {
                if (!IsLoaded) // loại trường hợp thực thi xự kiện này khi đang load form
                    return;
                if (rabtnInOrder.IsChecked != null) _isExPortOrder = !rabtnInOrder.IsChecked.Value;
                GetListOfOrderWithConditions();
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }

        private void BtnEditOrderClick(object sender, RoutedEventArgs e)
        {
            try
            {
                var button = sender as Button;
                if (button == null) return;
                var orderToEdit = button.DataContext as ModelOrderManagement;
                if (orderToEdit != null && orderToEdit.OrderType == 1)
                {
                    var fEditExport = new FormEditExportOrder(orderToEdit.IOrderID);
                    fEditExport.ShowDialog();
                    //getListOfOrderWithConditions();//refresh
                }
                else
                {
                    if (orderToEdit != null)
                    {
                        var fEditImport = new FormEditImportOrder(orderToEdit.IOrderID);
                        fEditImport.ShowDialog();
                    }
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }

        private void BtnSearchByUserClick(object sender, RoutedEventArgs e)
        {
            try 
            {
                
                int UserID;
                if(!int.TryParse(txtUserName.Text, out UserID)) // nếu không nhập số sẽ không làm gì hết
                    return;

                var catalogID = 0;
                if (lvCatalog.SelectedIndex > 0) // đã lựa chọn 1 catalog
                {
                    var catalogs = lvCatalog.SelectedItem as Catalogs;
                    if (catalogs != null)
                        catalogID = catalogs.IDCatalog;
                }

                _listOfBills = WPFServices.Services.OrderManagementService.GetListBillsByUser(UserID, dpickerFrom.SelectedDate, dpickerTo.SelectedDate, catalogID, _isExPortOrder);
                dtgOrderManagement.ItemsSource = _listOfBills;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

    }
}
